/**
 * (C) Copyright 2014 ZWW
 */
package cputils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.dom4j.DocumentException;
import org.jsoup.Connection;
import org.jsoup.HttpStatusException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

/**
 * @author ZWW
 * @date 2014-1-5
 * @version V1.0
 */
public class CPDataService {
	
	public static void main(String[] args) throws NumberFormatException, IOException, DocumentException {
		//Map<String, String> resMap = getDLTData("13153");
		while(true){
			System.out.print("Input keyword:");
			Scanner sca = new Scanner(System.in);  
			String queryStr = sca.next();
		}
	}
	
	public static Map<String, String> getSSQData(String issueNo){
		return readDataAicai("101", issueNo);
	}
	public static Map<String, String> getDLTData(String issueNo){
		return readDataAicai("201", issueNo);
	}
	//DLT 201  SSQ 101
	private static Map<String, String> readDataAicai(String gameIndex,String issueNo) {
		String url = "http://kaijiang.aicai.com/allopenprized/kaijiang.jhtml?action=historyIssue&gameIndex="+gameIndex+"&issueNo="
				+ issueNo;// +
		Connection conn = Jsoup.connect(url);
		String typeName = "";
		if ("101".equals(gameIndex)) {
			typeName = "ssq";
		}else if ("201".equals(gameIndex)) {
			typeName = "dlt";
		}
		Document doc = null;
		try {
			doc = conn.get();
		} catch (HttpStatusException e) {
			System.err.println(typeName+" number: "+issueNo+" is illegal.");
			XmlUtils.writeLogs(typeName+" number: "+issueNo+" is illegal.", CPDataService.class, null);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Map<String, String> resMap = null;
		if (doc!=null) {
			resMap = new HashMap<String, String>();
			JSONArray jsonArray = JSONArray.fromObject("[" + doc.text() + "]");
			JSONObject objRoot = null;
			for (int i = 0, l = jsonArray.size(); i < l; i++) {
				objRoot = jsonArray.getJSONObject(i);
				String reString = (String) objRoot.get("openResult");
				//String[] res = reString.split("</i>");
				resMap.put(issueNo, reString.replaceAll("</i>", " ").trim());
				//System.out.println(resMap);
			}
		}
		return resMap;
	}
}
